package com.example.service;

import com.example.entity.User;
import com.example.exception.UserNotFoundException;
import com.example.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;

import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;

/**
 * @Author:0xOO
 * @Date: 2018/9/26 0026
 * @Time: 15:23
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    SqlSessionFactory sqlSessionFactory;
    public User selectById(int id){
        User user = Optional.ofNullable(userMapper.selectById(id)).orElseThrow(
                () -> new UserNotFoundException(id)
        );
        return user;
    }

    /**
     * 查询用户前1000条
     * @return
     */
    public List<User> getUserList(){
        List<User> userList = userMapper.selectUser();
        return userList;
    }
    /**
     *  @Transactional 事务并不回滚，数据库中的数据发生了改变。这是因为 IOException 属于 checked 异常
     * @param user
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    public int insert(User user)  {
        userMapper.insert(user);
        return 1;
    }
    @Transactional(propagation = Propagation.NEVER)
    public int insertBatch(List<User> userList){
        Long start =  System.nanoTime();
//        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        for (int i = 0; i < userList.size(); i++) {
//            mapper.insert(userList.get(i));
//        }
//        sqlSession.flushStatements();

//        userMapper.insertBatch(userList);
        new ThreadPoolExecutor(1000,3000,1000L,
                java.util.concurrent.TimeUnit.MILLISECONDS,
                new java.util.concurrent.ArrayBlockingQueue<>(10),
                new ThreadPoolExecutor.AbortPolicy()).execute(()->{
            userMapper.insertBatchSQL(userList);
        });

        System.out.println("批量插入数据库耗时："+(System.nanoTime()-start));
        return 0;
    }
    public Page<User> findByPage(int pageNo, int pageSize) {
        //count false 表示不执行 select count(*) from user; 语句
//        PageHelper.startPage(pageNo, pageSize,false); //line 1
        int pageStart=0;
        if(pageNo>0){
            pageStart  = (pageNo-1)*pageSize;
        }
        return userMapper.findByPage(pageStart,pageSize); //line 2
    }

}
